<?php

namespace app\common\model;

use Endroid\QrCode\QrCode;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

class OrderRefund extends Model{

    use SoftDelete;
    // 表名
//    protected $name = 'order';
//    protected $pk = 'id';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    public function orderGoods()
    {
        return $this->hasMany('OrderGoods', 'order_id', 'id');
    }

    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        if(!$value){ return []; }
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            if($v && $v!='undefined') {
                $v=get_app_image_url($v);
            } else {
                unset($value[$k]);
            }
        }
        return $value;
    }

    public function getCreatetimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['createtime'] ? $data['createtime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }
    /**
     * 获取数据
     * */
    public static function getlist($where=[]){

        $list = self::where($where)
            ->alias('a')
            ->field('a.*,o.id as order_id,o.order_sn')
            ->order('a.id desc')
            ->join('order o','a.order_id=o.id')
//            ->join('shop_apply s','o.sell_id=s.id')
            ->paginate();
        foreach ($list as $k=>$v){
            $ordergoods = OrderGoods::where(['order_id'=>$v['order_id']])->select();
            foreach ($ordergoods as $kk=>$vv){
                $vv['thumbnail_image'] = $vv['images'];
            }
            $v['order_goods']=$ordergoods;
            $v['is_back']=1;
        }
        return $list;
    }
    /**
     * 订单详情
     * */
    public static function getInfo($where){
        $info = self::where($where)
            ->alias('a')
            ->field('a.*,o.id as order_id,o.order_sn')
            ->order('a.id desc')
            ->join('order o','a.order_id=o.id')
            ->find();

        $ordergoods = OrderGoods::where(['order_id'=>$info['order_id']])->select();
        foreach ($ordergoods as $kk=>$vv){
            $vv['thumbnail_image'] = $vv['images'];
        }
        $info['order_goods']=$ordergoods;
        $info['is_back']=1;
        return $info;
    }

}